/**
 * 
 */
package com.exemplo.flickrsocial.service;

import android.os.AsyncTask;
import android.util.Log;

import com.exemplo.flickrsocial.model.FlickrSocialModel;
import com.exemplo.flickrsocial.view.FlickrHelper;
import com.exemplo.flickrsocial.view.FlickrSocial;
import com.gmail.yuyang226.flickr.Flickr;
import com.gmail.yuyang226.flickr.oauth.OAuth;
import com.gmail.yuyang226.flickr.oauth.OAuthInterface;

/**
 * 
 * @author Douglas
 * 
 */
public class GetOAuthTokenTask extends AsyncTask<String, Integer, OAuth> {

	private FlickrSocial activity;
	private FlickrSocialModel flickrModel;

	public GetOAuthTokenTask(FlickrSocial context) {
		this.activity = context;
		this.flickrModel = new FlickrSocialModel(context);
	}

	@Override
	protected OAuth doInBackground(String... params) {
		String oauthToken = params[0];
		String oauthTokenSecret = params[1];
		String verifier = params[2];

		Flickr flickr = FlickrHelper.getInstance().getFlickr();
		OAuthInterface oauthApi = flickr.getOAuthInterface();

		try {
			return oauthApi.getAccessToken(oauthToken, oauthTokenSecret,
					verifier);
		} catch (Exception e) {

			Log.e(getClass().toString(), e.getMessage());

			return null;
		}

	}

	@Override
	protected void onPostExecute(OAuth oauth) {
		if (activity != null) {
			flickrModel.onOAuthDone(oauth);

			if (oauth != null) {

				activity.load(oauth);
			}
		}
	}

}
